﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using HiLand.Utility.Setting;

namespace HiLand.Utility.Module
{
    public class LoginValidateModule : IHttpModule
    {
        private HttpApplication context;
        #region IHttpModule 成员

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            this.context = context;
            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
        }

        void context_AuthenticateRequest(object sender, EventArgs e)
        {
            //value的格式为 目录;角色,角色||目录;角色,角色
            string validateInfoValue = Config.GetAppSetting("needValidateInfoValue").ToLower();
            string[] validateInfos = validateInfoValue.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < validateInfos.Length; i++)
            {
                string currentInfo = validateInfos[i];
                string[] currntValue = currentInfo.Split(';');
                if (currntValue.Length == 2)
                {
                    string validatePath = currntValue[0];
                    string[] validateRole = currntValue[1].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    Web.LoginValidation.ReadAndRedirect(validatePath, validateRole);
                }
            }
        }

        #endregion
    }
}
